package cn.tellsea.service.impl;

import cn.tellsea.entity.AjaxResult;
import cn.tellsea.entity.BizOrder;
import cn.tellsea.feignclient.FeignBizAccountService;
import cn.tellsea.feignclient.FeignBizStorageService;
import cn.tellsea.mapper.BizOrderMapper;
import cn.tellsea.service.IBizOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author Tellsea
 * @since 2021-12-31
 */
@Slf4j
@Service
public class BizOrderServiceImpl extends ServiceImpl<BizOrderMapper, BizOrder> implements IBizOrderService {

    @Autowired
    private FeignBizAccountService accountService;
    @Autowired
    private FeignBizStorageService storageService;

    @Override
    @GlobalTransactional(name = "tellsea_tx_group", rollbackFor = Exception.class)
    public AjaxResult createOrder(BizOrder entity) {
        log.info("------>开始新建订单");
        entity.setStatus(1);
        baseMapper.insert(entity);
        log.info("------>订单微服务开始调用库存，扣减count");
        storageService.decrease(entity.getProductId(), entity.getCount());
        log.info("------>订单微服务开始调用库存，扣减end");
        log.info("------>订单微服务开始调用账户，扣减money");
        accountService.decrease(entity.getUserId(), entity.getMoney());
        log.info("------>订单微服务开始调用账户，扣减end");
        log.info("------>开始修改订单状态");
        entity.setStatus(2);
        baseMapper.updateById(entity);
        log.info("------>修改订单状态完毕");
        log.info("------>下单完毕");
        return AjaxResult.success("下单成功");
    }
}
